package org.oschina.api.user.mail.service;

import org.oschina.Constants;
import org.oschina.api.BaseService;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.user.mail.dao.UserMailRecordDAO;
import org.oschina.api.user.mail.model.UserMailRecord;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserMailRecordService extends BaseService {
	
	@Autowired
	private UserMailRecordDAO userMailRecordDAO;
	@Autowired
	private IndexService indexService;

	/**
	 * 是否已读
	 * @param user
	 * @param mail
	 * @return
	 */
	public boolean isRead(int user,int mail)
	{
		return userMailRecordDAO.isRead(user, mail);
	}
	
	/**
	 * 创建
	 * @param user
	 * @param mail
	 */
	public void create(int user,int mail)
	{
		UserMailRecord userMailRecord=new UserMailRecord();
		userMailRecord.setUser(user);
		userMailRecord.setMail(mail);
		create(userMailRecord);
	}
	
	/**
	 * 创建
	 * @param userMailRecord
	 * @return
	 */
	private UserMailRecord create(UserMailRecord userMailRecord)
	{
		userMailRecord.setId(getIndex());
		userMailRecord.setCreate_time(DateUtil.now());
		return userMailRecordDAO.create(userMailRecord);
	}
	
	private int getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_USER_MAIL_RECORD);
	}
}